package com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.dataParser;

import com.alibaba.citrus.ots.common.util.SapAndCrmFieldDealUtils;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.dataParser.DataParser;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.constant.ConsignmentOrderLineDiffStandardFieldDict;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.FieldLineDTO;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapIf017Detail;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author: qiaojiange
 * @create: 2022/01/23
 * @Description sap017子单 数据解析器
 */
@Component
public class SapIf017DetailDataParse extends DataParser {
    /**
     * 报文中的字段，映射成对账处理的业务字段(标准字段)
     * key：报文中的字段，value：转换后的字段,这里的 value 字段也不要重复
     */
    private static Map<String, String> dataBaseFiledMapping = new HashedMap();

    static {
        //sap交货单号
        dataBaseFiledMapping.put("orderLineId", ConsignmentOrderLineDiffStandardFieldDict.SAP_ORDER_LINE_ID.getFieldCode());
        //sap报文中的销售单行号 对应了 外部销售单号 这个地方是取反的，要多加注意
        dataBaseFiledMapping.put("outerSoLineId", ConsignmentOrderLineDiffStandardFieldDict.OTS_SO_LINE_ID.getFieldCode());
        //sap报文中的外部销售单号 对应了 销售单号。 这个地方是取反的，要多加注意
        dataBaseFiledMapping.put("soLineId", ConsignmentOrderLineDiffStandardFieldDict.SAP_SO_LINE_ID.getFieldCode());
        dataBaseFiledMapping.put("scItemId", ConsignmentOrderLineDiffStandardFieldDict.SAP_SC_ITEM_ID.getFieldCode());
        dataBaseFiledMapping.put("qty", ConsignmentOrderLineDiffStandardFieldDict.QTY.getFieldCode());
        dataBaseFiledMapping.put("podStatus", ConsignmentOrderLineDiffStandardFieldDict.POD_STATUS.getFieldCode());
        dataBaseFiledMapping.put("podQty", ConsignmentOrderLineDiffStandardFieldDict.POD_QTY.getFieldCode());
        dataBaseFiledMapping.put("factoryCode", ConsignmentOrderLineDiffStandardFieldDict.FACTORY_CODE.getFieldCode());
        dataBaseFiledMapping.put("inventoryCode", ConsignmentOrderLineDiffStandardFieldDict.INVENTORY_CODE.getFieldCode());
        dataBaseFiledMapping.put("saleUnit", ConsignmentOrderLineDiffStandardFieldDict.SALE_UNIT.getFieldCode());
        dataBaseFiledMapping.put("basicQuantity", ConsignmentOrderLineDiffStandardFieldDict.BASIC_QUANTITY.getFieldCode());
    }


    @Override
    public Map<String, FieldLineDTO> parse(Object obj) {
        Map<String, FieldLineDTO> map = new HashMap<>();
        if (obj instanceof MnSapIf017Detail) {
            MnSapIf017Detail sapIf017Detail = (MnSapIf017Detail) obj;
            map = obtainFieldLineMap(dataBaseFiledMapping, sapIf017Detail, sapIf017Detail.getClass());

            //特殊处理外部行号
            FieldLineDTO fieldLineDTO = map.get(ConsignmentOrderLineDiffStandardFieldDict.SAP_ORDER_LINE_ID.getFieldCode());
            fieldLineDTO.setFieldValue(SapAndCrmFieldDealUtils.generateIdField(fieldLineDTO.getFieldValue()));

            //平台销售单行号
            fieldLineDTO = map.get(ConsignmentOrderLineDiffStandardFieldDict.OTS_SO_LINE_ID.getFieldCode());
            fieldLineDTO.setFieldValue(SapAndCrmFieldDealUtils.generateIdField(fieldLineDTO.getFieldValue()));

            //sap销售单行号
            fieldLineDTO = map.get(ConsignmentOrderLineDiffStandardFieldDict.SAP_SO_LINE_ID.getFieldCode());
            fieldLineDTO.setFieldValue(SapAndCrmFieldDealUtils.generateIdField(fieldLineDTO.getFieldValue()));

            //特殊处理商品 ID
            fieldLineDTO = map.get(ConsignmentOrderLineDiffStandardFieldDict.SAP_SC_ITEM_ID.getFieldCode());
            fieldLineDTO.setFieldValue(SapAndCrmFieldDealUtils.generateScItemIdField(fieldLineDTO.getFieldValue()));
        }
        return map;
    }

    @Override
    public String obtainProcessType() {
        return "MnSapIf017Detail";
    }
}
